﻿using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using ThridGroup.ERP.DTO.ProductionSystem;
using ThridGroup.ERP.ProductionSystem;

namespace ThridGroup.ERP.Web.Controllers.ProductionSystem
{
    public class CrewSchedulingController : Controller
    {
        public IActionResult CrewSchedulingIndex()
        {
            return View();
        }


       

        private readonly ICrewSchedulingServices _crewSchedulingServices;

        public CrewSchedulingController(ICrewSchedulingServices crewSchedulingServices)
        {
            _crewSchedulingServices = crewSchedulingServices;
        }

        /// <summary>
        /// 添加排班计划
        /// </summary>
        /// <param name="crewScheduling"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> AddCrewScheduling([FromBody]CrewSchedulingDTO crewScheduling)
        {
            return await _crewSchedulingServices.AddCrewScheduling(crewScheduling);
        }

        /// <summary>
        /// 删除排班计划
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task DelCrewScheduling([FromBody]List<int> ids)
        {
            await _crewSchedulingServices.DelCrewScheduling(ids);
        }
        /// <summary>
        /// 获取排班计划信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<CrewSchedulingDTO> GetCrewScheduling(int id)
        {
            return  await _crewSchedulingServices.GetCrewScheduling(id);
        }
        /// <summary>
        /// 显示排班计划列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="planName"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<JsonResult> ShowCrewScheduling(int pageIndex = 1, int pageSize = 10, string? planName = "")
        {
            var crew = await _crewSchedulingServices.ShowCrewScheduling(pageIndex, pageSize, planName);
            var result = crew.OrderByDescending(m => m.Id).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return Json(new
            {
                code=0,
                msg="",
                count=crew.Count,
                data=result
            });
        }
        /// <summary>
        /// 编辑排班计划列表
        /// </summary>
        /// <param name="crewScheduling"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<int> UpdateCrewScheduling([FromBody]CrewSchedulingDTO crewScheduling)
        {
            return await _crewSchedulingServices.UpdateCrewScheduling(crewScheduling);
        }

        public async Task<IActionResult> EditCrewSchedulingView(int? id)
        {
            CrewSchedulingDTO model = new CrewSchedulingDTO();
            if (id.HasValue && id.Value > 0)
            {
                model = await _crewSchedulingServices.GetCrewScheduling(id.Value);
            }
            return View(model);
        }

        //===================================班次=======================================
        /// <summary>
        /// 添加班次
        /// </summary>
        /// <param name="sailings"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> AddSailings([FromBody]SailingsModelDTO sailings)
        {
            return await _crewSchedulingServices.AddSailings(sailings);
        }
        /// <summary>
        /// 显示班次列表
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<JsonResult> ShowSailings(int? id=0)
        {
            var list= await _crewSchedulingServices.ShowSailings(id);
            return Json(new
            {
                code=0,
                msg="",
                data=list
            });
        }
        /// <summary>
        /// 删除班次
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task DeleteSailings([FromBody]List<int> ids)
        {
           await _crewSchedulingServices.DeleteSailings(ids);
        }
        /// <summary>
        /// 获取班次
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<SailingsModelDTO> GetSailings(int id)
        {
            return await _crewSchedulingServices.GetSailings(id);
        }
        /// <summary>
        /// 编辑班次
        /// </summary>
        /// <param name="sailings"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<int> UpdateSailings([FromBody]SailingsModelDTO sailings)
        {
            return await _crewSchedulingServices.UpdateSailings(sailings);
        }


    }
}
